package com.lenovo.manufacture.Fragment;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.lenovo.manufacture.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.GET;

/**
 * A simple {@link Fragment} subclass.
 */
public class gonhuo extends Fragment {
    private int o = 0, h = 0;
    private Myadpter myadpter = new Myadpter();
    private List<gonhuoshujv> list = new ArrayList<>();
    @BindView(R.id.gonhuo_tv1)
    TextView gonhuoTv1;
    @BindView(R.id.gonhuo_tv2)
    TextView gonhuoTv2;
    @BindView(R.id.gonhuo_listview)
    ListView gonhuoListview;

    public gonhuo() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_gonhuo, container, false);
        ButterKnife.bind(this, inflate);
        inst();
        return inflate;
    }

    public interface Bolg {
        @GET("getchargelog")
        Call<ResponseBody> getBolg();
    }

    public void inst() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://hh1.me:5001/").build();
        Bolg bolg = retrofit.create(Bolg.class);
        Call<ResponseBody> call = bolg.getBolg();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Gson gson = new Gson();

                try {
                    gonhuoshujv gonhuoshujv = gson.fromJson(response.body().string(), gonhuoshujv.class);
                    list.add(gonhuoshujv);
                    gonhuoListview.setAdapter(myadpter);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    @OnClick({R.id.gonhuo_tv1, R.id.gonhuo_tv2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.gonhuo_tv1:
                o += 1;
                if (o == 1) {
                    Collections.sort(list.get(0).getData(), new Comparator<gonhuoshujv.DataBean>() {
                        @Override
                        public int compare(gonhuoshujv.DataBean o1, gonhuoshujv.DataBean o2) {
                            return (int) (o1.getAmount() - o2.getAmount());
                        }
                    });
                } else {
                    Collections.sort(list.get(0).getData(), new Comparator<gonhuoshujv.DataBean>() {
                        @Override
                        public int compare(gonhuoshujv.DataBean o1, gonhuoshujv.DataBean o2) {
                            return (int) (o2.getAmount() - o1.getAmount());
                        }
                    });
                    o = 0;
                }
                break;
            case R.id.gonhuo_tv2:
                h = +1;
                if (h == 1) {
                    Collections.sort(list.get(0).getData(), new Comparator<gonhuoshujv.DataBean>() {
                        @Override
                        public int compare(gonhuoshujv.DataBean o1, gonhuoshujv.DataBean o2) {
                            return (int) (o1.getCharge() - o2.getCharge());
                        }
                    });
                } else {
                    Collections.sort(list.get(0).getData(), new Comparator<gonhuoshujv.DataBean>() {
                        @Override
                        public int compare(gonhuoshujv.DataBean o1, gonhuoshujv.DataBean o2) {
                            return (int) (o2.getCharge() - o1.getCharge());
                        }
                    });
                    h = 0;
                }
                break;
        }
    }

    public class Myadpter extends BaseAdapter {
        @Override
        public int getCount() {
            return list.get(0).getData().size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @SuppressLint("ViewHolder")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View inflate = View.inflate(getContext(), R.layout.gonhuo_list, null);
            ViewHolder viewHolder = new ViewHolder(inflate);
            viewHolder.gonhuoListTv1.setText(list.get(0).getData().get(position).getWho() + "");
            viewHolder.gonhuoListTv2.setText(list.get(0).getData().get(position).getAmount() + "");
            viewHolder.gonhuoListTv3.setText(list.get(0).getData().get(position).getCharge() + "");
            viewHolder.gonhuoListTv4.setText(list.get(0).getData().get(position).getCarno() + "");
            return inflate;
        }


        class ViewHolder {
            @BindView(R.id.gonhuo_list_tv1)
            TextView gonhuoListTv1;
            @BindView(R.id.gonhuo_list_tv2)
            TextView gonhuoListTv2;
            @BindView(R.id.gonhuo_list_tv3)
            TextView gonhuoListTv3;
            @BindView(R.id.gonhuo_list_tv4)
            TextView gonhuoListTv4;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
}
